<?php
/**
 * User: roman
 * Date: 22.06.11
 * Time: 17:33
 */

namespace Managers;

class Items extends BaseManager {

    protected $__table = '\Catalog\Items';

    public function getById($id) {

        $res = $this->_em->createQueryBuilder()
                ->select('i')
                ->from($this->__table, 'i')
                ->innerJoin('i.tag', 'tags')
                ->where('i.id = :id')
                ->setParameter('id', 1)
                ->getQuery()
                ->getArrayResult();

        return $res[0] ? : false;
    }

    public function getOneItem($id) {

       $res = $this->_em->createQueryBuilder()

                    ->select('con', 'i', 'tag', 'gr', 'u.name','u.id')
                    ->from('\Catalog\Connect', 'con')
                    ->join('con.item', 'i')
                    ->join('con.tag', 'tag')
                    ->join('tag.group', 'gr')

                    ->join('i.user', 'u')

                    ->where('i.id = :id')

                     ->setParameter('id', $id)
                    ->getQuery()
                    ->getArrayResult();


        
                    $item = new \Catalog\Items();
                    $item = $this->_em->find('\Catalog\Items',$id);
                    $views=$item->getViews()+1;
                    $item->setViews($views);
                    $this->_em->persist($item);
                    $this->_em->flush();


                    return $res;




    }

    public function add($name) {

        $item = new \Catalog\Items();
        $item->setName($name);

        $this->_em->persist($item);
        $this->_em->flush();

        return $item;
    }

    public function getList() {

//        $res = $this->_getCacheManager()->load('items');
//die('ok');
//        if (!$res) {

            $res = $this->_em->createQueryBuilder()
                    ->select('i','image')
                    ->from($this->__table, 'i')
                    ->leftJoin('i.image', 'image' )
                    ->getQuery()
                    ->getArrayResult();

//            $this->_getCacheManager()->save($res, 'items', array(), 60);
//        }
//print_r($res);
  //      die;
        return $res;
    }

    public function ary2() {

        $res = $this->_em->createQueryBuilder()
                ->select('con', 'item', 'tag')
                ->from('\Catalog\Connect', 'con')
                ->join('con.item', 'item')
                ->join('con.tag', 'tag')
                ->getQuery()
                ->getArrayResult();

        return $res;

    }

    public function ary() {


        $res = $this->_em->createQueryBuilder()
                ->select('it')
                ->from($this->__table, 'it')
                ->getQuery()
                ->getArrayResult();


        foreach ($res as $key => $value) {

            $res[$key]['tags'] = $this->_em->createQueryBuilder()
                    ->select('tag', 'con')
                    ->from('\Catalog\Connect', 'con')
                    ->join('con.tag', 'tag')
                    ->where('con.item = :id')
                    ->setParameter('id', $value['id'])
                    ->getQuery()
                    ->getArrayResult();

        }
        return $res;

    }

    public function getListItemTag() {

        $res = $this->_em->createQueryBuilder()
                ->select('item')
                ->from($this->__table, 'item')
                ->getQuery()
                ->getArrayResult();


        foreach ($res as $key => $value) {

            $tagsIDs = array();

            $tags = $this->_em->createQueryBuilder()
                    ->select('con', 'tag')
                    ->from('\Catalog\Connect', 'con')
                    ->join('con.tag', 'tag')
                    ->where('con.item = :id')
                    ->setParameter('id', $value['id'])
                    ->getQuery()
                    ->getArrayResult();

            foreach ($tags as $tag) {

                $tagsIDs[] = $tag['tag']['id'];

            }

            $res[$key]['tags'] = $tagsIDs;

        }

        return $res;
    }


}
